﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Data.SQLite;

namespace SqliteWrapper.Procedures
{
    public class Proc_InsertBook : IProcedure
    {
        public override SqliteError doProcedure(object DataSource, string DBPath)
        {
            Book book = DataSource as Book;
            SqliteError error = new SqliteError() { IsProcessSuccess = false, ErrorText = null };
            SqliteConnectionManager manager = SqliteConnectionManager.Create(DBPath);
            using (SQLiteWriteLock writeLock = new SQLiteWriteLock(manager.ConnectionString))
            {
                using (SQLiteConnection conn = manager.GetSqliteConnection())
                {
                    conn.Open();
                    using (SQLiteCommand cmd = conn.CreateCommand())
                    {
                        cmd.CommandText = "select count(*) from Book where Title = @Title and Author = @Author and CategoryId = @CategoryId and AlbumId = @AlbumId";
                        cmd.Parameters.Add("@Title", System.Data.DbType.String).Value = book.Title;
                        cmd.Parameters.Add("@Author", System.Data.DbType.String).Value = book.Author;
                        cmd.Parameters.Add("@AlbumId", System.Data.DbType.Int64).Value = book.AlbumId;
                        cmd.Parameters.Add("@CategoryId", System.Data.DbType.Int64).Value = book.CategoryId;
                        int count = Convert.ToInt32(cmd.ExecuteScalar());
                        if (count == 0)
                        {
                            cmd.Parameters.Clear();
                            cmd.CommandText = "insert into Book(Title,Author,SourcePath,AlbumId,CategoryId,CreateTimeTag,Desc,Thumbnail,Version) " +
                                              "values(@Title,@Author,@SourcePath,@AlbumId,@CategoryId,@CreateTimeTag,@Desc,@Thumbnail,@Version)";
                            cmd.Parameters.Add("@Title", System.Data.DbType.String).Value = book.Title;
                            cmd.Parameters.Add("@Author", System.Data.DbType.String).Value = book.Author;
                            cmd.Parameters.Add("@SourcePath", System.Data.DbType.String).Value = book.SourcePath;
                            cmd.Parameters.Add("@AlbumId", System.Data.DbType.Int64).Value = book.AlbumId;
                            cmd.Parameters.Add("@CategoryId", System.Data.DbType.Int64).Value = book.CategoryId;
                            cmd.Parameters.Add("@CreateTimeTag", System.Data.DbType.String).Value = book.CreateTimeTag;
                            cmd.Parameters.Add("@Desc", System.Data.DbType.String).Value = book.Desc;
                            cmd.Parameters.Add("@Thumbnail", System.Data.DbType.String).Value = book.Thumbnail;
                            cmd.Parameters.Add("@Version", System.Data.DbType.Double).Value = book.Version;
                            cmd.CommandType = System.Data.CommandType.Text;
                            try
                            {
                                cmd.ExecuteNonQuery();
                                error.IsProcessSuccess = true;
                            }
                            catch (Exception ex)
                            {
                                error.ErrorText = ex.Message;
                            }
                        }
                        else
                        {
                            error.IsProcessSuccess = false;
                            error.ErrorText = "书籍已存在";
                        }
                    }
                }
            }
            return error;
        }
    }
}
